/*
 * Created by Jaren at 2021/8/17 16:02
 */
class LogUtils {
  static LogLevel level = LogLevel.debug;

  static void v(dynamic message, [dynamic error]) {
    if (level.index >= LogLevel.verbose.index) log(LogLevel.verbose, message, error);
  }

  static void d(dynamic message, [dynamic error]) {
    if (level.index >= LogLevel.debug.index) log(LogLevel.debug, message, error);
  }

  static void i(dynamic message, [dynamic error]) {
    if (level.index >= LogLevel.info.index) log(LogLevel.info, message, error);
  }

  static void w(dynamic message, [dynamic error]) {
    if (level.index >= LogLevel.warning.index) log(LogLevel.warning, message, error);
  }

  static void e(dynamic message, [dynamic error]) {
    if (level.index >= LogLevel.error.index) log(LogLevel.error, message, error);
  }

  static void log(LogLevel level, dynamic message, [dynamic error]) {
    assert(() {
      var msg = message.toString();
      if (msg.length > 800) {
        int line = msg.length ~/ 800 + 1;
        for (var i = 0; i < line; i++) {
          print("${DateTime.now().toString()} [${level.name}] ${msg.substring(i * 800)}");
        }
      } else {
        print("${DateTime.now().toString()} [${level.name}] ${message.toString()}");
      }

      if (error != null) print(error);
      return true;
    }());
  }
}

/// 日志等级
/// e > w > d > i > v > none
enum LogLevel { error, warning, debug, info, verbose, none }
